package com.jay.codec

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

/**
 * @author xuzhou4
 * @date 2021/12/27
 */
object CodecSettings {

    const val SP_FILE_NAME = "av_codec_settings"
    const val KEY_MODE_CODEC = "KEY_MODE_CODEC"
    const val MODE_CODEC_HARDWARE = 1
    const val MODE_CODEC_SOFTWARE = 2

    var mSharedPreferences: SharedPreferences? = null

    private fun initPreference(context: Context) {
        mSharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE)
    }

    private fun checkPreference(context: Context) {
        if (mSharedPreferences == null) {
            initPreference(context.applicationContext)
        }
    }

    @SuppressLint("CommitPrefEdits")
    private fun putString(key: String, value: String?) {
        mSharedPreferences?.edit()?.putString(key, value)?.apply()
    }

    private fun getString(key: String, defaultValue: String?): String? {
        return mSharedPreferences?.getString(key, defaultValue)
    }

    private fun getString(key: String): String? {
        return getString(key, null)
    }

    @SuppressLint("CommitPrefEdits")
    private fun putInt(key: String, value: Int) {
        mSharedPreferences?.edit()?.putInt(key, value)?.apply()
    }

    private fun getInt(key: String, defaultValue: Int): Int? {
        return mSharedPreferences?.getInt(key, defaultValue)
    }

    private fun getInt(key: String): Int? {
        return getInt(key, 0)
    }

    fun setCodecMode(context: Context, codecMode: Int) {
        checkPreference(context)
        putInt(KEY_MODE_CODEC, codecMode)
    }

    fun getCodecMode(context: Context): Int {
        checkPreference(context)
        return getInt(KEY_MODE_CODEC, 0) ?: 0
    }
}